<template>
  <div class="container">
    <xy-table ref="table" :api="api" :columns="columns" :on-edit="onEdit">
      <template slot="addItem">
        <div>
          <el-button @click="onAdd" class="ml0" size="mini">
            <i class="el-icon-plus"></i>
            添加地址</el-button
          >
        </div>
      </template>
    </xy-table>
    <el-dialog :visible.sync="dlgVisible" :title="dlgTitle" width="62%">
      <el-form
        ref="formRef"
        :model="addForm"
        label-width="7rem"
        class="grid-flex"
        :rules="$rules"
      >
        <template v-for="(item, key) in addForm">
          <template v-if="key === 'IsDefault'">
            <el-form-item :key="key" :label="$language.local(key)" :prop="key">
              <el-switch
                v-model="addForm[key]"
                :active-value="1"
                :inactive-value="0"
              ></el-switch>
            </el-form-item>
          </template>
          <template v-else-if="key === 'MID'">
            <el-form-item :key="key" :label="$language.local(key)" :prop="key">
              <select-merchantid v-model="addForm[key]"></select-merchantid>
            </el-form-item>
          </template>
          <template v-else-if="/Id|Indate/.test(key)"></template>
          <el-form-item
            v-else
            :key="key"
            :label="$language.local(key)"
            :prop="key"
          >
            <xy-input v-model="addForm[key]"></xy-input>
          </el-form-item>
        </template>
      </el-form>
      <template slot="footer">
        <div class="form-footer">
          <el-button @click="reload">取消</el-button>
          <el-button @click="onSubmit('formRef')" type="primary"
            >确定</el-button
          >
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import API from "../../plugins/api";
export default {
  inject: ["reload"],
  data() {
    return {
      api: new API("/api/invoiceaddress/GetList"),
      columns: {
        // Id: 6,
        // MID: 112,
        RecipientName: "罗娟",
        Phone: "17773434957",
        RecipientAddr: "收件地址",
        IsDefault: 1,
        Indate: "2021-01-27T14:02:27.953",
      },
      addForm: {
        MID: "",
        RecipientName: "",
        RecipientAddr: "",
        Phone: "",
        IsDefault: 1,
      },
      dlgVisible: false,
      dlgTitle: "添加地址",
    };
  },
  created() {},
  methods: {
    onSearch(e) {
      this.$refs.table.getData(e);
    },
    onAdd() {
      this.api.fullName = "/api/invoiceaddress/Create";
      this.dlgVisible = true;
    },
    async onSubmit(formName) {
      await this.$refs[formName].validate();
      await this.$http.post(this.api.fullName, this.addForm);
      this.reload();
    },
    onEdit(row) {
      console.log(row);
      this.addForm = { ...row };
      this.api.fullName = "/api/invoiceaddress/Edit";

      this.dlgVisible = true;
    },
  },
};
</script>

<style></style>
